package com.allwees.bs.task.order.auto;

import com.allwees.bs.core.model.order.config.ScheduleTimeProperties;
import com.allwees.bs.module.fms.req.DoSettleReq;
import com.allwees.bs.module.fms.service.RetailerSettleStatementService;
import com.allwees.bs.module.fms.service.SupplierSettleStatementService;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.bs.module.oms.service.GeneralOmsOrderService;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.common.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;


/**
 * 资金结算
 */
@Slf4j
@Service

public class OrderSettledTask {

    @Autowired
    private OmsOrderItemDao omsOrderItemDao;
    @Autowired
    private GeneralOmsOrderService generalOmsOrderItemService;
    @Autowired
    private SupplierSettleStatementService supplierSettleStatementService;
    @Autowired
    private RetailerSettleStatementService retailerSettleStatementService;
    @Autowired
    private ScheduleTimeProperties scheduleTimeProperties;

    @XxlJob(value="orderSettledTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        //已结算金额
        doSettledAmount();
    }




    /**
     *  已结算金额
     */
    private  void doSettledAmount() {
        if (scheduleTimeProperties.getOrderAutoToSettledSecond() == null) {
            return;
        }
        log.info("===========[orderSettledTask start]=======================");
        //List<OmsOrderItem> omsOrderItems = omsOrderItemDao.findCanSettleds(DateUtil.addSeconds(new Date(), -scheduleTimeProperties.getOrderAutoToSettledSecond()), EStatus.OMS_TO_SETTLE.value());
        List<OmsOrderItem> omsOrderItems = omsOrderItemDao.findCanSettleds(EStatus.OMS_TO_SETTLE.value());

        if (CollectionUtils.isEmpty(omsOrderItems)) {
            log.info("===========[orderSettledTask end , oms_to_settle orders not find ]=======================");
            return;
        }
        Date  toSettleDate =  DateUtil.addSeconds(new Date(), -scheduleTimeProperties.getOrderAutoToSettledSecond());

        omsOrderItems.forEach(o -> {
            if(toSettleDate.after(o.getJoinSettleAt())) {//大于30天结算日
                log.info("===========[doSettledAmount,after join_settle_at] orderItemUuid: {},join_settle_at:{},toSettleDate:{},review_at:{}",o.getUuid(),o.getJoinSettleAt(),toSettleDate,o.getReviewAt());
                if (StringUtils.isNotBlank(o.getRetailerUuid())) {
                    retailerSettleStatementService.doSettle(o,new DoSettleReq(o.getRetailerUuid()),true);
                } else if (StringUtils.isNotBlank(o.getSupplierUuid())) {
                    supplierSettleStatementService.doSettle(o,new DoSettleReq(o.getSupplierUuid()),true);
                }
            } else {//小于30天结算日
                log.info("===========[doSettledAmount,before join_settle_at] orderItemUuid: {},join_settle_at:{},toSettleDate:{},review_at:{}",o.getUuid(),o.getJoinSettleAt(),toSettleDate,o.getReviewAt());
                if(null !=  o.getReviewAt() ) {///小于结算日但是已评价
                    if (StringUtils.isNotBlank(o.getRetailerUuid())) {
                        retailerSettleStatementService.doSettle(o,new DoSettleReq(o.getRetailerUuid()),false);
                    } else if (StringUtils.isNotBlank(o.getSupplierUuid())) {
                        supplierSettleStatementService.doSettle(o,new DoSettleReq(o.getSupplierUuid()),false);
                    }
                }
            }

        });

       // Set<String> orderUuids = omsOrderItems.stream().map(OmsOrderItem::getOrderUuid).collect(Collectors.toSet());
       // generalOmsOrderItemService.doSettled(orderUuids);
        log.info("===========[orderSettledTask end]=======================");
    }
}
